home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / mac / DirectX SDK / DXSDK / samples / Multimedia / VBSamples / DirectPlay / Conferencer / modDplay.bas < prev    next >
BASIC Source File  |  2001-10-08  |  12KB  |  329 lines

  1. Attribute VB_Name = "modDplay"
  2. Option Explicit
  3. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  4. '
  5. '  Copyright (C) 1999-2001 Microsoft Corporation.  All Rights Reserved.
  6. '
  7. '  File:       modDPlay.bas
  8. '
  9. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  10.  
  11. 'Here are all of the messages we can transfer in this app
  12. Public Enum vbMsgType
  13.     MsgChat 'We are talking in the chat channel
  14.     MsgWhisper 'We are whispering to someone in the chat channel
  15.     MsgAskToJoin 'We want to ask if we can join this session
  16.     MsgAcceptJoin 'Accept the call
  17.     MsgRejectJoin 'Reject the call
  18.     MsgCancelCall 'Cancel the call
  19.     MsgShowChat 'Show the chat window
  20.     MsgSendFileRequest 'Request a file transfer
  21.     MsgSendFileAccept 'Accept the file transfer
  22.     MsgSendFileDeny 'Deny the file transfer
  23.     MsgSendFileInfo 'File information (size)
  24.     MsgSendFilePart 'Send a chunk of the file
  25.     MsgAckFilePart 'Acknowledge the file part
  26.     MsgSendDrawPixel 'Send a drawn pixel
  27.     MsgSendDrawLine 'Send a drawn line
  28.     MsgShowWhiteBoard 'Show the whiteboard window
  29.     MsgClearWhiteBoard 'Clear the contents of the whiteboard
  30.     MsgNewPlayerJoined 'A new player has joined our session
  31. End Enum
  32.  
  33. 'Win32 declares
  34. Public Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
  35. Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
  36. Public Declare Function Shell_NotifyIcon Lib "shell32.dll" Alias "Shell_NotifyIconA" (ByVal dwMessage As Long, lpData As NOTIFYICONDATA) As Long
  37. Public Declare Sub InitializeCriticalSection Lib "kernel32" (lpCriticalSection As CRITICAL_SECTION)
  38. Public Declare Sub LeaveCriticalSection Lib "kernel32" (lpCriticalSection As CRITICAL_SECTION)
  39. Public Declare Sub EnterCriticalSection Lib "kernel32" (lpCriticalSection As CRITICAL_SECTION)
  40. Public Declare Sub DeleteCriticalSection Lib "kernel32" (lpCriticalSection As CRITICAL_SECTION)
  41.  
  42. Public Type CRITICAL_SECTION
  43.     DebugInfo As Long
  44.     LockCount As Long
  45.     RecursionCount As Long
  46.     OwningThread As Long
  47.     LockSemaphore As Long
  48.     SpinCount As Long
  49. End Type
  50.  
  51. Public Type NOTIFYICONDATA
  52.     cbSize As Long
  53.     hwnd As Long
  54.     uID As Long
  55.     uFlags As Long
  56.     uCallbackMessage As Long
  57.     hIcon As Long
  58.     sTip As String * 64
  59. End Type
  60.     
  61. Public Const NIM_ADD = &H0
  62. Public Const NIM_MODIFY = &H1
  63. Public Const NIM_DELETE = &H2
  64. Public Const NIF_MESSAGE = &H1
  65. Public Const NIF_ICON = &H2
  66. Public Const NIF_TIP = &H4
  67. Public Const NIF_DOALL = NIF_MESSAGE Or NIF_ICON Or NIF_TIP
  68. Public Const WM_MOUSEMOVE = &H200
  69. Public Const WM_LBUTTONDBLCLK = &H203
  70. Public Const WM_RBUTTONUP = &H205
  71.  
  72. 'Constants
  73. Public Const AppGuid = "{9073823A-A565-4865-87EC-19B93B014D27}"
  74. Public Const glDefaultPort As Long = 9897
  75.  
  76. 'DirectX variables
  77. Public dx As DirectX8
  78. Public dpp As DirectPlay8Peer
  79. Public dvClient As DirectPlayVoiceClient8
  80. Public dvServer As DirectPlayVoiceServer8
  81.  
  82. 'Window variables for this app
  83. Public ChatWindow As frmChat
  84. Public WhiteBoardWindow As frmWhiteBoard
  85. Public NetWorkForm As frmNetwork
  86.  
  87. 'Misc app variables
  88. Public sysIcon As NOTIFYICONDATA
  89. Public gsUserName As String
  90. Public glAsyncEnum As Long
  91. Public glMyPlayerID As Long
  92. Public glHostPlayerID As Long
  93. Public gfHost As Boolean
  94. Public gfNoVoice As Boolean
  95. Public goSendFile As CRITICAL_SECTION
  96. Public goReceiveFile As CRITICAL_SECTION
  97.  
  98. Public Sub Main()
  99.     If App.PrevInstance Then
  100.         'We can only run one instance of this sample per machine since we
  101.         'specify a port to run this application on.  Only one application can
  102.         'be listening (hosting) on a particular port at any given time.
  103.         MsgBox "Only one instance of vbConferencer may be run at a time.", vbOKOnly Or vbInformation, "Only one"
  104.         Exit Sub
  105.     End If
  106.     Screen.MousePointer = vbHourglass
  107.     'Show the splash screen
  108.     frmSplash.Show
  109.     'Set our username up
  110.     gsUserName = GetSetting("VBDirectPlay", "Defaults", "UserName", vbNullString)
  111.     If gsUserName = vbNullString Then
  112.         'If there is not a default username, then pick the currently
  113.         'logged on username
  114.         gsUserName = Space$(255)
  115.         GetUserName gsUserName, 255
  116.         gsUserName = Left$(gsUserName, InStr(gsUserName, Chr$(0)) - 1)
  117.     End If
  118.     'Start the host
  119.     Set NetWorkForm = New frmNetwork
  120.     Load NetWorkForm
  121.     'We don't need it anymore
  122.     Unload frmSplash
  123.     Screen.MousePointer = vbNormal
  124.     NetWorkForm.Show vbModeless
  125.     InitializeCriticalSection goSendFile
  126.     InitializeCriticalSection goReceiveFile
  127. End Sub
  128.  
  129. Public Sub InitDPlay()
  130.     Set dx = New DirectX8
  131.     Set dpp = dx.DirectPlayPeerCreate
  132. End Sub
  133.  
  134. Public Sub Cleanup()
  135.     On Error Resume Next
  136.     'We might have references for the chat and whiteboard windows
  137.     'Get rid of them
  138.     Set ChatWindow = Nothing
  139.     Set WhiteBoardWindow = Nothing
  140.     'Disconnect and destroy the client
  141.     If Not (dvClient Is Nothing) Then
  142.         dvClient.UnRegisterMessageHandler
  143.         dvClient.Disconnect DVFLAGS_SYNC
  144.         Set dvClient = Nothing
  145.     End If
  146.     'Stop and Destroy the server
  147.     If Not (dvServer Is Nothing) Then
  148.         dvServer.UnRegisterMessageHandler
  149.         dvServer.StopSession 0
  150.         Set dvServer = Nothing
  151.     End If
  152.     'Now the main session
  153.     If Not (dpp Is Nothing) Then
  154.         dpp.UnRegisterMessageHandler
  155.         'Close our peer connection
  156.         dpp.Close
  157.         'Lose references to peer object
  158.         Set dpp = Nothing
  159.     End If
  160.     'Lose references to dx object
  161.     Set dx = Nothing
  162.     DoSleep 500
  163. End Sub
  164.  
  165. Public Sub StartHosting(MsgForm As Form)
  166.     Dim dpa As DirectPlay8Address
  167.     Dim oPlayer As DPN_PLAYER_INFO
  168.     Dim oAppDesc As DPN_APPLICATION_DESC
  169.     
  170.     'Make sure we're ready to host
  171.     Cleanup
  172.     InitDPlay
  173.     NetWorkForm.cmdHangup.Enabled = False
  174.     NetWorkForm.cmdCall.Enabled = True
  175.     gfHost = True
  176.     'Register the Message Handler
  177.     dpp.RegisterMessageHandler MsgForm
  178.     'Set the peer info
  179.     oPlayer.lInfoFlags = DPNINFO_NAME
  180.     oPlayer.Name = gsUserName
  181.     dpp.SetPeerInfo oPlayer, DPNOP_SYNC
  182.     'Create an address
  183.     Set dpa = dx.DirectPlayAddressCreate
  184.     'We will only be connecting via TCP/IP
  185.     dpa.SetSP DP8SP_TCPIP
  186.     dpa.AddComponentLong DPN_KEY_PORT, glDefaultPort
  187.     
  188.     'First set up our application description
  189.     With oAppDesc
  190.         .guidApplication = AppGuid
  191.         .lMaxPlayers = 10 'We don't want to overcrowd our 'room'
  192.         .lFlags = DPNSESSION_NODPNSVR
  193.     End With
  194.     'Start our host
  195.     dpp.Host oAppDesc, dpa
  196.     Set dpa = Nothing
  197.         
  198.     'After we've created the session and let's start
  199.     'the DplayVoice server
  200.     Dim oSession As DVSESSIONDESC
  201.  
  202.     'Create our DPlayVoice Server
  203.     Set dvServer = dx.DirectPlayVoiceServerCreate
  204.  
  205.     'Set up the Session
  206.     oSession.lBufferAggressiveness = DVBUFFERAGGRESSIVENESS_DEFAULT
  207.     oSession.lBufferQuality = DVBUFFERQUALITY_DEFAULT
  208.     oSession.lSessionType = DVSESSIONTYPE_PEER
  209.     oSession.guidCT = vbNullString
  210.  
  211.     'Init and start the session
  212.     dvServer.Initialize dpp, 0
  213.     dvServer.StartSession oSession, 0
  214.     ConnectVoice MsgForm
  215.     Set dpa = Nothing
  216. End Sub
  217.  
  218. Public Sub Connect(MsgForm As Form, ByVal sHost As String)
  219.     Dim dpa As DirectPlay8Address
  220.     Dim dpl As DirectPlay8Address
  221.     Dim oPlayer As DPN_PLAYER_INFO
  222.     Dim oAppDesc As DPN_APPLICATION_DESC
  223.     
  224.     'Try to connect to the host
  225.     'Make sure we're ready to connect
  226.     Cleanup
  227.     InitDPlay
  228.     NetWorkForm.cmdCall.Enabled = False
  229.     gfHost = False
  230.     'Register the Message Handler
  231.     dpp.RegisterMessageHandler MsgForm
  232.     'Set the peer info
  233.     oPlayer.lInfoFlags = DPNINFO_NAME
  234.     oPlayer.Name = gsUserName
  235.     dpp.SetPeerInfo oPlayer, DPNOP_SYNC
  236.     'Now try to enum hosts
  237.     
  238.     'Create an address
  239.     Set dpa = dx.DirectPlayAddressCreate
  240.     'We will only be connecting via TCP/IP
  241.     dpa.SetSP DP8SP_TCPIP
  242.     dpa.AddComponentString DPN_KEY_HOSTNAME, sHost 'We will try to connect to this host
  243.     dpa.AddComponentLong DPN_KEY_PORT, glDefaultPort
  244.     
  245.     Set dpl = dx.DirectPlayAddressCreate
  246.     'We will only be connecting via TCP/IP
  247.     dpl.SetSP DP8SP_TCPIP
  248.     
  249.     'First set up our application description
  250.     With oAppDesc
  251.         .guidApplication = AppGuid
  252.     End With
  253.     'Try to connect to this host
  254.     On Error Resume Next
  255.     DoSleep 500 'Give a slight pause to clean up any loose ends
  256.     dpp.Connect oAppDesc, dpa, dpl, 0, ByVal 0&, 0
  257.     If Err.Number <> 0 Then 'Woah, an error
  258.         MsgBox "There was an error trying to connect to this machine.", vbOKOnly Or vbInformation, "Unavailable"
  259.         StartHosting MsgForm
  260.     End If
  261.     Set dpa = Nothing
  262.     Set dpl = Nothing
  263. End Sub
  264.  
  265. Public Sub ConnectVoice(MsgForm As Form)
  266.     Dim oSound As DVSOUNDDEVICECONFIG
  267.     Dim oClient As DVCLIENTCONFIG
  268.     
  269.     'Make sure we haven't determined there would be no voice in this app
  270.     If gfNoVoice Then Exit Sub
  271.     'Now create a client as well (so we can both talk and listen)
  272.     Set dvClient = dx.DirectPlayVoiceClientCreate
  273.     'Now let's create a client event..
  274.     dvClient.Initialize dpp, 0
  275.     dvClient.StartClientNotification MsgForm
  276.     'Set up our client and sound structs
  277.     oClient.lFlags = DVCLIENTCONFIG_AUTOVOICEACTIVATED Or DVCLIENTCONFIG_AUTORECORDVOLUME
  278.     oClient.lBufferAggressiveness = DVBUFFERAGGRESSIVENESS_DEFAULT
  279.     oClient.lBufferQuality = DVBUFFERQUALITY_DEFAULT
  280.     oClient.lNotifyPeriod = 0
  281.     oClient.lThreshold = DVTHRESHOLD_UNUSED
  282.     oClient.lPlaybackVolume = DVPLAYBACKVOLUME_DEFAULT
  283.     oSound.hwndAppWindow = NetWorkForm.hwnd
  284.     
  285.     On Error Resume Next
  286.     'Connect the client
  287.     dvClient.Connect oSound, oClient, 0
  288.     If Err.Number = DVERR_RUN_SETUP Then    'The audio tests have not been run on this
  289.                                             'machine.  Run them now.
  290.         'we need to run setup first
  291.         Dim dvSetup As DirectPlayVoiceTest8
  292.         
  293.         Set dvSetup = dx.DirectPlayVoiceTestCreate
  294.         dvSetup.CheckAudioSetup vbNullString, vbNullString, NetWorkForm.hwnd, 0 'Check the default devices since that's what we'll be using
  295.         If Err.Number = DVERR_COMMANDALREADYPENDING Then
  296.             MsgBox "Could not start DirectPlayVoice.  The Voice Networking wizard is already open.  This sample will not have any voice capablities.", vbOKOnly Or vbInformation, "No Voice"
  297.             gfNoVoice = True
  298.             NetWorkForm.chkVoice.Value = vbUnchecked
  299.             NetWorkForm.chkVoice.Enabled = False
  300.             Exit Sub
  301.         End If
  302.         If Err.Number = DVERR_USERCANCEL Then
  303.             MsgBox "Could not start DirectPlayVoice.  The Voice Networking wizard was cancelled.  This sample will not have any voice capablities.", vbOKOnly Or vbInformation, "No Voice"
  304.             gfNoVoice = True
  305.             NetWorkForm.chkVoice.Value = vbUnchecked
  306.             NetWorkForm.chkVoice.Enabled = False
  307.             Exit Sub
  308.         End If
  309.         Set dvSetup = Nothing
  310.         dvClient.Connect oSound, oClient, 0
  311.     ElseIf Err.Number <> 0 And Err.Number <> DVERR_PENDING Then
  312.         MsgBox "Could not start DirectPlayVoice.  This sample will not have any voice capablities." & vbCrLf & "Error:" & CStr(Err.Number), vbOKOnly Or vbInformation, "No Voice"
  313.         gfNoVoice = True
  314.         NetWorkForm.chkVoice.Value = vbUnchecked
  315.         NetWorkForm.chkVoice.Enabled = False
  316.         Exit Sub
  317.     End If
  318.     On Error GoTo 0
  319. End Sub
  320.  
  321. Public Sub DoSleep(ByVal lNumMS As Long)
  322.     Dim lCount As Long
  323.     
  324.     For lCount = 1 To lNumMS \ 5
  325.         Sleep 5
  326.         DoEvents
  327.     Next
  328. End Sub
  329.